--[[
	Name: GridStatusHostileUnit
	Revision: $Revision: 82146 $
	Developed by: Julith @ Perenolde-EU (billgatesgut@web.de)
	Description: Publishes the state if a Unit is hostile to the player.

	Credits to oodyboo from WoWAce for the idea of this addon.
]]

--{{{ Libraries

local L = AceLibrary("AceLocale-2.2"):new("GridStatusHostileUnit")

--}}}

L:RegisterTranslations("enUS", function()
	return {
		    ["HostileUnit"] = true,
        ["hostile unit"] = true,
        ["WarningNotice"] = "Note from GridStatusHostileUnit:\n\n"..
                            "To reduce CPU-usage overhead of the last version I've changed some things which might "..
                            "result in not noticed hostile units or (worse) errors. If this happens, please let me "..
                            "know about it in the WowAce-Forums, I'll fix it as fast as I can.\n\nThank you for your patience in advance.",
	}
end)

L:RegisterTranslations("koKR", function()
	return {
		["HostileUnit"] = "적대적 유닛",
        ["hostile unit"] = "적대적 유닛",
	}
end)

L:RegisterTranslations("zhCN", function()
	return {
		["HostileUnit"] = "敌对单位",
        ["hostile unit"] = "敌对单位",
	}
end)

L:RegisterTranslations("zhTW", function()
	return {
		["HostileUnit"] = "敵對單位",
        ["hostile unit"] = "敵對單位",
	}
end)

L:RegisterTranslations("frFR", function()
	return {
		    ["HostileUnit"] = "Unit\195\169 hostile",
        ["hostile unit"] = "Unit\195\169 hostile",
        ["WarningNotice"] = "Note from GridStatusHostileUnit:\n\n"..
                            "To reduce CPU-usage overhead of the last version I've changed some things which might "..
                            "result in not noticed hostile units or (worse) errors. If this happens, please let me "..
                            "know about it in the WowAce-Forums, I'll fix it as fast as I can.\n\nThank you for your patience in advance.",
	
	}
end)

L:RegisterTranslations("ruRU", function()
	return {
		    ["HostileUnit"] = "Враждебный",
        ["hostile unit"] = "враждебный игрок",
        ["WarningNotice"] = "Добавляет для Grid Статус Игрока ставшего враждебным:\n\n"..
                            "Чтобы уменьшить CPU-производительность я изменил некторые вщи в аддоне и теперь он более легкий "..
                            "если увидите ошибки в аддоне . Сообщите мне "..
                            "Я живу на WowAce-Forums, Я исправлюошибку быстро.\n\nБлагодарю заранее за ваше терпение.",
	}
end)

GridStatusHostileUnit = GridStatus:NewModule("GridStatusHostileUnit")
GridStatusHostileUnit.menuName = L["HostileUnit"]


--{{{ AceDB defaults

GridStatusHostileUnit.defaultDB = {
	unitIsHostile = {
		enable = true,
		priority = 90,
		color = { r = 1, g = 0, b = 0, a = 1 },
	},
    warningDisplayed = false,
}

GridStatusHostileUnit.options = false

--}}}

--{{{ Options
local amountOptions = {
}
--}}}

local playerIsHostile = false
local settings

function GridStatusHostileUnit:OnInitialize()
	self.super.OnInitialize(self)
	self:RegisterStatus("unitIsHostile", L["hostile unit"], amountOptions, true)
    settings = GridStatusHostileUnit.db.profile
end

function GridStatusHostileUnit:OnEnable()
    --self:RegisterEvent("UNIT_TARGET", "UNIT_AURA")
	self:RegisterEvent("UNIT_AURA", "UNIT_AURA")
    self:RegisterEvent("Grid_UnitJoined")
	self:RegisterEvent("Grid_UnitChanged")
--[[    
    if not settings.warningDisplayed then
        StaticPopupDialogs["GRID_HOSTILE_NOTE"] = {
          text = L["WarningNotice"],
          button1 = "Ok.",
          timeout = 0,
          whileDead = 1,
        };
        StaticPopup_Show ("GRID_HOSTILE_NOTE");
        settings.warningDisplayed = true;
    end
]]
end

function GridStatusHostileUnit:Grid_UnitJoined(unitGUID, unitid)
	self:Update(unitid)
end

function GridStatusHostileUnit:Grid_UnitChanged(unitGUID, unitid)
    self:Update(unitid)
end

function GridStatusHostileUnit:Update(unitid)
    if not unitid then
        return
    end

    local gridName = UnitGUID(unitid);

    --local hostile = UnitIsUnit("player", unitid.."target");
    local hostile = UnitIsCharmed(unitid) --or UnitCanAttack(unitid, "player");
    
    if hostile then
        self.core:SendStatusGained(gridName, "unitIsHostile",
				    settings.unitIsHostile.priority,
				    nil,
				    settings.unitIsHostile.color)
    else
        self.core:SendStatusLost(gridName, "unitIsHostile")
    end
end

function GridStatusHostileUnit:UNIT_AURA(unitid)    
    self:Update(unitid)
end


